package com.tablefood.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import com.tablefood.service.ProductService;
import com.tablefood.utils.PageProductBean;
import com.tablefood.dao.ProductDao;
import com.tablefood.domain.ShopProduct;

@Transactional
public class ProductServiceImpl implements ProductService {

	@Resource
	ProductDao productDao;
	
	//添加商品
	@Override
	public boolean addProduct(ShopProduct product) {
		return productDao.addProduct(product);
	}

	 
	//删除商品
	@Override
	public boolean delProduct(Long productid) {
		return productDao.delProducts(productid);
	}

	/*
	 * 根据productid查询某件商品
	 * */
	@Override
	public ShopProduct searchProducts(Long productid) {
		System.out.println("查询某件商品");
		return productDao.searchProducts(productid);
	}

 
	@Override
	public boolean  updateProduct(ShopProduct product) {
		return productDao.updateProduct(product);
	}
	
	/*根据页面所需大小计算所需页面数量*/
	@Override
	public int CountProductPage(String categoryName,int pageSize) {
	
	     int count=0;                                            //商品数量，初始为0个商品记录
		 int pageCount=1;                                        //页面总数，初始只有一页

	     count=productDao.getProductsCount(categoryName);                    //获得商品记录数
		 pageCount=(count%pageSize==0)?(count/pageSize):(count/pageSize+1);  //计算应有页面数量

		 return pageCount;
	}

	/*根据categoryName种类名称查询特定商品集合*/
	@Override
	public List searchProducts(String categoryName, int pageIndex, int pageSize) {
		return productDao.searchProducts(categoryName,pageIndex,pageSize);
	}

	//通过商品名字查找商品
	@Override
	public boolean checkProductTitle(String title) {
		return productDao.checkProductTitle(title);
	}


	@Override
	public PageProductBean queryForPage(int pageSize, int page) {

		final String hql = "from ShopProduct u where state = '1' order by u.productid desc"; // 查询语句
		int allRow = productDao.getAllRowCount(hql); // 总记录数
		int totalPage = PageProductBean.countTatalPage(pageSize, allRow); // 总页数
		final int offset = PageProductBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageProductBean.countCurrentPage(page); // 当前页
		List list = productDao.queryForPage(hql, offset, length); //

		// 把分页信息保存到Bean当中
		PageProductBean pageProductBean = new PageProductBean();
		pageProductBean.setPageSize(pageSize);
		pageProductBean.setCurrentPage(currentPage);
		pageProductBean.setAllRow(allRow);
		pageProductBean.setTotalPage(totalPage);
		pageProductBean.setList(list);
		pageProductBean.init();
		return pageProductBean;
	}



	@Override
	public int getProductsCount(String categoryName) {
		return productDao.getProductsCount(categoryName);
	}

	@Override
	public PageProductBean searchForPage(String search ,int pageSize, int page) {
		 
		String hql = "from ShopProduct   a  where a.title like :param"; // 查询语句
		 
		int allRow = productDao.getAllRowCount(hql,search); // 总记录数
		int totalPage = PageProductBean.countTatalPage(pageSize, allRow); // 总页数
		final int offset = PageProductBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageProductBean.countCurrentPage(page); // 当前页
		List list = productDao.queryForPage(hql, search, offset, length); //

		// 把分页信息保存到Bean当中
		PageProductBean pageProductBean = new PageProductBean();
		pageProductBean.setPageSize(pageSize);
		pageProductBean.setCurrentPage(currentPage);
		pageProductBean.setAllRow(allRow);
		pageProductBean.setTotalPage(totalPage);
		pageProductBean.setList(list);
		pageProductBean.init();
		return pageProductBean;
		 
	}

}
